package com.colin.aiyi.ui.sayDetail;

import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.colin.aiyi.R;
import com.colin.aiyi.presenter.AbsP;
import com.colin.aiyi.app.Constant;
import com.colin.aiyi.app.MyApp;
import com.colin.aiyi.dao.ImpxUtils.ImpDoWatch;
import com.colin.aiyi.dao.ImpxUtils.ImpSayCommentList;
import com.colin.aiyi.dao.ImpxUtils.ImpSayDetail;
import com.colin.aiyi.entity.BaseRequest;
import com.colin.aiyi.entity.SayDetail;
import com.colin.aiyi.entity.User;
import com.colin.aiyi.entity.response.WatchResponse;
import com.colin.aiyi.entity.YiJuAndYiShuoEntity;
import com.colin.aiyi.utils.MusicUtil;
import com.colin.aiyi.utils.SharePreferenceUtil;

/**
 * Created by hl on 2017/6/1.
 */

public class ImpPSayDetail extends AbsP {
    SayDetail sayDetail;
    IViewDetail iViewDetail;
    private YiJuAndYiShuoEntity childYiShuoEntity;


    public void setiViewDetail(IViewDetail iViewDetail) {
        this.iViewDetail = iViewDetail;
    }

    @Override
    public void onSuccess(Object t) {
        super.onSuccess(t);
       if (t instanceof SayDetail){//详情结果集
           sendMsgToView(t);
           this.sayDetail= (SayDetail) t;
           return;
       }
        if (t instanceof WatchResponse){
            if (((WatchResponse) t).getCode()==200) {
                if (iViewDetail.isChildClick()){
                    MusicUtil.getInstance().setDataSource(
                            iViewDetail.getContext(),
                            childYiShuoEntity.getVoiceUrl() + ((WatchResponse) t).getData().getToken());
                }else {
                    MusicUtil.getInstance().setDataSource(
                            iViewDetail.getContext(),
                            sayDetail.getData().getVoiceUrl() + ((WatchResponse) t).getData().getToken());
                }
            }
        }

    }

    @Override
    public <V> void say_getDetail(V v) {
        super.say_getDetail(v);
        iService=new ImpSayDetail();
        iService.getObject(this,v);

    }


    @Override
    public <V> void say_commentList(V v) {
        super.say_commentList(v);
        iService=new ImpSayCommentList();
        iService.getObject(v);
    }

    /********
     * 观看接口
     * @param v
     * @param <V>
     */
    @Override
    public <V> void say_watch(V v) {
        super.say_watch(v);
        iService=new ImpDoWatch();
        iService.getObject(this,v);
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        MusicUtil.getInstance().stop();
        childYiShuoEntity = (YiJuAndYiShuoEntity) adapter.getData().get(position);
        MusicUtil.getInstance().setMusicTitle(childYiShuoEntity.getTitle());
        MusicUtil.getInstance().setMusicPic(Constant.BASE_URL + childYiShuoEntity.getUrl());
        if (!iViewDetail.checkLogin()){
            return;
            }
            if (!childYiShuoEntity.isFree() || !childYiShuoEntity.isIsFree()) {
                if (!iViewDetail.checkUserVip()){
                    return;
                }
            }
            User user = SharePreferenceUtil.getObject(iViewDetail.getContext(), User.class);
            user.setSayId(childYiShuoEntity.getId() + "");
            say_watch(user);
            iViewDetail.setChildClick(true);
            MusicUtil.getInstance().sendStartEvent();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v.getId()== R.id.music_play){
            if (!iViewDetail.checkLogin()){
                return;
            }
            if (!sayDetail.getData().isFree() || !sayDetail.getData().isIsFree()) {
                if (!iViewDetail.checkUserVip()){
                    return;
                }
            }
            if (MusicUtil.getInstance().getMusicPic()!=null&&!MusicUtil.getInstance().getMusicPic().equals(Constant.BASE_URL + sayDetail.getData().getUrl())){
                MusicUtil.getInstance().stop();
            }
            if (iViewDetail.isChildClick()){
                MusicUtil.getInstance().sendStopEvent();
                iViewDetail.setChildClick(false);
                BaseRequest request= new BaseRequest();
                request.setSayId(iViewDetail.getId());
                say_watch(request);
                MusicUtil.getInstance().sendStartEvent();
            }else {
                if (MusicUtil.getInstance().isPlaying()) {
                    MusicUtil.getInstance().pause();
                }else if (MusicUtil.getInstance().isPause() ) {
                    MusicUtil.getInstance().play();
                }else {
                    User baseRequest=SharePreferenceUtil.getObject(MyApp.getInstance().getApplicationContext(),User.class);
                    baseRequest.setSayId(iViewDetail.getId());
                    say_watch(baseRequest);
                    MusicUtil.getInstance().sendStartEvent();
                }
            }
            MusicUtil.getInstance().setMusicTitle(sayDetail.getData().getTitle());
            MusicUtil.getInstance().setMusicPic(Constant.BASE_URL + sayDetail.getData().getUrl());
        }
    }
}
